Note
Go to the end to download the full example code
Streak Detection and Centroiding#
Doing image processing to figure out where the streaks are on a FITS image
import os
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation, PillowWriter
import mirage as mr
import mirage.vis as mrv
fits_path = os.path.join(os.environ["SRCDIR"], "..", "00161295.48859.fit")
info = mr.info_from_fits(fits_path)
img = info["ccd_adu"]
img_raw = img.copy()
img_log10 = np.log10(img)
img = np.log10(np.clip(img - mr.image_background_parabola(img), 1, np.inf))
total_pix_tracked = info["total_pix_tracked"]
img[img < 1] = 0
img[np.isnan(img) | np.isinf(np.abs(img))] = 0
streak_length = 100
Demonstrating the dependence of convolved variance on template streak direction
plt.figure(figsize=(15, 5))
thetas = np.linspace(0, np.pi, 30, endpoint=False)
vars = np.zeros_like(thetas)
img_for_gif = img[::10, ::10]
def animate(i):
global vars
dir = np.array([np.cos(thetas[i]), np.sin(thetas[i])])
kernel = mr.streak_convolution_kernel(dir, streak_length)
conv_img = mr.streak_convolution(img_for_gif, dir, streak_length)
vars[i] = np.var(conv_img)
plt.subplot(1, 3, 1)
plt.gca().cla()
plt.imshow(conv_img)
mrv.texit(f"Convolved Image", "", "", grid=False)
plt.subplot(1, 3, 2)
plt.gca().cla()
plt.imshow(kernel)
mrv.texit(rf"Streak Kernel $\theta={thetas[i]:2.2f}$ [rad]", "", "", grid=False)
plt.subplot(1, 3, 3)
plt.gca().cla()
plt.plot(thetas[: i + 1], vars[: i + 1])
plt.xlim(0, np.pi)
plt.ylim(0, 0.025)
mrv.texit(
f"Convolved Image Variance", "Streak angle [rad]", "Variance [ndim]", grid=True
)
plt.pause(0.01)
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, repeat=True, frames=thetas.size, interval=50)
writer = PillowWriter(fps=10, bitrate=1800)
writer.setup(ani, "streaks.gif", dpi=200)
ani.save("streaks.gif", writer=writer)
plt.show()